import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

class Student {
    public int studentId;
    public int classId;
}

@WebServlet("/getparameter2")
public class PostParameter2 extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 通过 getInputStream 把 req 对象里的 body 完整读取出来
        // 在流对象中读取多少个字节，取决于 Content-Length
//        int length = req.getContentLength(); // 这是 body 实际的长度
//        // 创建一个相同长度的字节数组
//        byte[] buffer = new byte[length];
//        // 借出 inputStream 来读取 body 的内容
//        InputStream inputStream = req.getInputStream();
//        inputStream.read(buffer);
//
//        // 把这个字节数组转成 String，然后打印出来
//        String body = new String(buffer, 0, length, "utf8");
//        System.out.println("body =" + body);

        // 使用 jackson 涉及到的核心对象
        ObjectMapper objectMapper = new ObjectMapper();
        // readValue 可以把一个 jackson 格式的字符串转成 java 对象
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
        System.out.println(student.studentId + "," + student.classId);
        //resp.getWriter().write(body);
    }
}
